// Function of the game 
function go(can,dim){
	
	var _taille;
	var _el;
	var _tour;
	var _ctx;
	var _Width;
    var _Height;
    var _coteTaille;
    var _grille;
    
	this.init = function() {
		console.log("init : " + this._taille);
		this._grille = new Array(this._taille);
		
		for(var i=0;i<this._taille;i++)
			this._grille[i]= new Array(this._taille);
		
		for(var i=0;i<this._taille;i++)
			for(var j=0;j<this._taille;j++)
				this._grille[i][j] = -1;
		
		this.InitSize();
		this.iniDraw();
		
		var that = this;
		that._el.addEventListener('click', function(ev) { that.ev_mousemove(ev); },  false);
		window.setInterval(function() { that.verif();}, 15);
	 };
    
	 this.verif = function(){
		 // fonction qui va verifier si le deuxieme joueur a jouer.
	 };
	 
	// Manage the mouse move
      this.ev_mousemove = function(ev) {
    	  var x, y;
    	  // Get the mouse position relative to the canvas element.
    	  if (ev.layerX || ev.layerX == 0) { // Firefox
    	    x = ev.layerX;
    	    y = ev.layerY;
    	  } else if (ev.offsetX || ev.offsetX == 0) { // Opera
    	    x = ev.offsetX;
    	    y = ev.offsetY;
    	  }
    	
    	  y=Math.round(((y - this._el.offsetTop)/(this._coteTaille/(this._taille+1))));
    	  x=Math.round(((x - this._el.offsetLeft)/(this._coteTaille/(this._taille+1))));
    	  
    	  if(this.isFree(x,y)) {
 			  if(this._tour){  // NOIR
 			    		if(this.degredeliberte(x-1, y-1, 0))  {
 							this._grille[(x-1)][(y-1)] = 0;
 							this._tour = false;
 						}
 						else console.info("IMPOSSIBLE DE PLACER LE PION ICI !!!");
 				}		
 				else{ // BLANC
 			    		if(this.degredeliberte(x-1, y-1, 1))  {
 							this._grille[(x-1)][(y-1)] = 1;
 							this._tour = true;
 						}
 						else console.info("IMPOSSIBLE DE PLACER LE PION ICI !!!");
 				}	
     	 }
 		

    	 this.iniDraw();
       };
       
	
       this.degredeliberte = function(abs, ord, couleur)
       {
	   		var liberte = false;
	   		
	   		if(ord>0){ // pour regarder en haut, il ne faut pas qu'on soit en haut
	   		
			   		console.log("je regarde en haut " + this._grille[abs][ord-1]);
			   			if(this._grille[abs][ord-1] == -1)
			   				liberte = true;
			   			else if(this._grille[abs][ord-1]==couleur)
			   				liberte = this.degredeliberte(abs, ord-1, couleur);
			   		}
			   		
			   		if(!liberte && ord+1<this._taille){ // en bas
			   		console.log("je regarde en bas");
			   			if(this._grille[abs][ord+1] == -1)
			   				liberte = true;
			   			else if(this._grille[abs][ord+1]==couleur)
			   				liberte = this.degredeliberte(abs, ord+1, couleur);
			   		}
			   		
			   		if(!liberte && abs>0){ // à gauche
			   		console.log("je regarde à gauche");
			   			if(this._grille[abs-1][ord] == -1)
			   				liberte = true;
			   			else if(this._grille[abs-1][ord]==couleur)
			   				liberte = this.degredeliberte(abs-1, ord, couleur);
			   		}
			   		if(!liberte && abs+1<this._taille){ // à droite
			   		console.log("je regarde à droite");
			   			if(this._grille[abs+1][ord]==-1)
			   				liberte = true;
			   			else if(this._grille[abs+1][ord]==couleur)
			   				liberte = this.degredeliberte(abs+1, ord, couleur);
	   		}
	   		
	   		return liberte;
   	};
   	
   	this.capture = function(abs, ord, couleur)
   	{
   		var ennemi;
   		
   		if (couleur==0) ennemi = 1;
   		else ennemi = 0;
   			
   		if(ord>0)
   			if(this._grille[abs][ord-1]== ennemi)
   				if (degredeliberte(abs, ord-1, ennemi))
   					this.effacerlachaine(abs, ord-1, ennemi);
   			
   		if(abs>0)
   			if(this._grille[abs-1][ord]== ennemi)
   				if (degredeliberte(abs-1, ord, ennemi))
   					this.effacerlachaine(abs-1, ord, ennemi);
   		
   		if(ord<=this._taille)
   			if(this._grille[abs][ord+1]== ennemi)
   				if (degredeliberte(abs, ord+1, ennemi))
   					this.effacerlachaine(abs, ord+1, ennemi);
   		
   		if(abs<=this._taille)
   			if(this._grille[abs+1][ord]== ennemi)
   				if (degredeliberte(abs+1, ord, ennemi))
   					this.effacerlachaine(abs+1, ord, ennemi);
   	};
   	
	this.effacerlachaine = function(abs, ord, couleur)
	{
	   		this._grille[abs][ord] = -1;
	   		
	   		if(ord>0)
	   			if(this._grille[abs][ord-1] == couleur)
	   				effacerlachaine(abs, ord-1, couleur);
	   		
	   		if(abs>0)
	   			if(this._grille[abs-1][ord] == couleur)
	   				effacerlachaine(abs-1, ord, couleur);
	   		
	   		if(ord<=this._taille)
	   			if(this._grille[abs][ord+1] == couleur)
	   				effacerlachaine(abs, ord+1, couleur);
	   		
	   		if(abs<=this._taille)
	   			if(this._grille[abs][ord+1] == couleur)
	   				effacerlachaine(abs, ord+1, couleur);
	};
   	
       // Place or not a ship
   	   this.isFree = function(x,y){
    	   if((x-1)>=this._taille || (x-1)<0 || (y-1)>=this._taille || (y-1)<0)
    		   return 0;
    		   
    	   if(this._grille[(x-1)][(y-1)]==-1)
    		   return 1;
    	   else
    		   return 0;   
       };
       
		// the ship
       this.Jeton = function(couleur,x,y) {
        	 if(couleur!=-1)
        	 {
    	      this._ctx.fillStyle = (couleur) ? "#FFFFFF" : "#000000";
        	  this._ctx.beginPath();
        	  this._ctx.arc(x*this._coteTaille/(this._taille+1),y*this._coteTaille/(this._taille+1),this._coteTaille/(this._taille+1)/2.5,0,Math.PI*2,true); // Cercle extérieur
        	  this._ctx.fill();
        	 }
       };
        
		// Manage the size of the board
	   this.InitSize = function() {
		  if( typeof( window.innerWidth ) == 'number' ) {//Non-IE
			    this._Width = window.innerWidth;
			    this._Height = window.innerHeight;
			    
			  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			    //IE 6+ in 'standards compliant mode'
			    this._Width = document.documentElement.clientWidth;
			    this._Height = document.documentElement.clientHeight;
			  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			    //IE 4 compatible
			    this._Width = document.body.clientWidth;
			    this._Height = document.body.clientHeight;
			  }
		
		  	this._ctx = this._el.getContext('2d');
			this._coteTaille = (this._Width>this._Height)? this._Height : this._Width;
			this._el.height = this._coteTaille;
			this._el.width = this._coteTaille;
		};
		
		this.iniDraw = function()
		{
	    	 // Make sure we don't execute when canvas isn't supported
	    	 if (this._el.getContext){
	    		 
	    		 // clean view
	    		 this._ctx.clearRect(0,0,this._coteTaille,this._coteTaille);
	    		 
	    	    // use getContext to use the canvas for drawing
	    	   this._ctx.beginPath();
	    	   
	     	   for (i = 0; i < (this._taille); i++){
	     		  this._ctx.moveTo(this._coteTaille/(this._taille+1),this._coteTaille/(this._taille+1)*(i+1));
	     		  this._ctx.lineTo(this._coteTaille-(this._coteTaille/(this._taille+1)),this._coteTaille/(this._taille+1)*(i+1));
	     		  this._ctx.moveTo(this._coteTaille/(this._taille+1)*(i+1),this._coteTaille/(this._taille+1));
	     		  this._ctx.lineTo(this._coteTaille/(this._taille+1)*(i+1),this._coteTaille-(this._coteTaille/(this._taille+1)));
	 	          }
	     	  this._ctx.stroke();
	        }
	    	 
	    	for(var i=0;i<this._taille;i++)
	    		for(var j=0;j<this._taille;j++)
	    			this.Jeton(this._grille[i][j],i+1,j+1);
		};
	
		this._taille = dim;
		this._el = can;
		this._tour = true;
}